<!DOCTYPE html>
<html>
	<head>
		<#include "/templates/commons/head.html">
		<script type="text/javascript">
			var Template;
		    require(['${ctx!}/plugins/art-template/template.js'], function(template) {
				Template = template;
		    });
		</script>
		<script type="text/html" id="tableBodyTemplate">
			{{each data as menuBean index}}
				<tr>
					<td>
						<div class="fn-hide">
							<input type="checkbox" class="fn-hide" name="menuIds" value="{{menuBean.menuId}}"/>
						</div>
						{{menuBean.menuName}}
					</td>
					<td>
						<input type="checkbox" lay-skin="primary" name="selectRole" {{if menuBean.selectRole == 1}}checked="checked"{{/if}} value="{{menuBean.menuId}}" />
						<div class="fn-hide">
							<input type="checkbox" class="fn-hide" disabled="disabled" checked="checked"/>
						</div>
					</td>
					<td>
						<input type="checkbox" lay-skin="primary" {{if menuBean.addRole == 1}}checked="checked"{{/if}} name="addRole" value="{{menuBean.menuId}}" />
					</td>
					<td>
						<input type="checkbox" lay-skin="primary" {{if menuBean.updateRole == 1}}checked="checked"{{/if}} name="updateRole" value="{{menuBean.menuId}}" />
					</td>
					<td>
						<input type="checkbox" lay-skin="primary" {{if menuBean.deleteRole == 1}}checked="checked"{{/if}} name="deleteRole" value="{{menuBean.menuId}}" />
					</td>
					<td>
						<input type="checkbox" lay-skin="primary" {{if menuBean.importRole == 1}}checked="checked"{{/if}} name="importRole" value="{{menuBean.menuId}}" />
					</td>
					<td>
						<input type="checkbox" lay-skin="primary" {{if menuBean.exportRole == 1}}checked="checked"{{/if}} name="exportRole" value="{{menuBean.menuId}}" />
					</td>
				</tr>
			{{/each}}
		</script>
		<style type="text/css">
			.layui-table-header .layui-table-cell .layui-form-checkbox {
				top: -1px !important;
			}
		</style>
		<script type="text/javascript">
			/**
			*角色列表页面点击保存，执行添加或修改角色操作
			*/
			function saveRole(){
			  	if(!$("#addForm").validateForm()){
					return;
			   	}
			   	var method = document.addForm.method.value;
				var messages = "";
				if(method=="addSave"){
					messages = "您是否确定要添加？";  
				}else if(method=="editSave"){
					messages = "您是否确定要修改？";
				}
				layer.confirm(messages, {
				    	btn: ['确定', '取消']
					}, function() {
					    var mythod = document.addForm.method.value;
						document.addForm.action ="${ctx!}/RoleController/"+mythod+".do";
						document.addForm.submit();
					}, function() {
				});
			}
			/**
			* 返回方法
			*/
		 	function back(){
			    document.addForm.action = "${ctx!}/RoleController/getRoleList.do";
			    document.addForm.submit();
		    }
			
			function initLayTable() {
			    var $table = $(".data-grid-group").find("table.dymanic-table");
				var $th = $table.find("th");
				$.each($th, function(i) {
					var $o = $(this);
					if ($o.find("input[type='checkbox']").length > 0) {
						var checkbox = $o.html();
						$(".layui-table-header").find("table").find("th").eq(i).find("div").html(checkbox);
					}
				});
			}
			
			function ready() {
			    $(".layui-table-body").css("width", "100%");
			    table.init('data-grid', {
				    height: document.body.clientHeight - 349//设置高度
				});
			    initLayTable();
				form.render();
				LayerConfig.hideTableScroll();
				//防止出现两个相同name的checkbox导致提交两遍
				$("#dataBody").empty();
			    form.on('radio(chooseOrg)', function(data) {
					//获取菜单列表
					var url = "${ctx!}/RoleController/getRoleMenuList.do"
					var params = {"orgId":data.value,"method": $("input[name=method]").val(), "roleId": $("input[name=roleId]").val()};
					var res = new Object();
					Common.network(url, "POST", params, false, function(data) {
					    res.data = data;
					})
					$("#dataBody").empty();//清空数据列表
					var tplHtml = Template("tableBodyTemplate", res);
					$("#dataBody").html(tplHtml);
					table.init('data-grid', {
					    height: document.body.clientHeight - 349//设置高度
					});
					initLayTable();
					form.render();
					LayerConfig.hideTableScroll();
					//防止出现两个相同name的checkbox导致提交两遍
					$("#dataBody").empty();
					//重新绑定全选事件
					form.on('checkbox(allChoose)', function(data){
						var chooseName = $(data.elem).attr("name").replace(/all/, "").replace(/(\w)/,function(v){return v.toLowerCase()});
						var child = $('tbody input[type="checkbox"][name="' + chooseName + '"]');
						child.each(function(index, item){
							if (typeof $(item).attr("disabled") == 'undefined') {
								item.checked = data.elem.checked;
							}
						});
						form.render('checkbox');
					});
			    })
			}
			
		</script>
	</head>
	<body>
		<div id="div-body" class="body">
			<div class="hid-scroller-body">
				<form id="addForm" class="layui-form" name="addForm" method="post">
					<!-- add||edit页面信息Start -->
					<#if method == 'add'>
						<input type="hidden" name="method" value="addSave"/>
					</#if>
					<#if method=='edit'>
						<input type="hidden" name="method" value="editSave"/>
					</#if>
					<!-- add||edit页面信息End -->
					<!-- list页面搜索条件 Start -->
					<input type="hidden" name="roleId" value="${roleBean.roleId!}"/>
					<!-- list页面搜索条件 End -->
					<!-- 添加信息 Form Start -->
					<div class="main-container">
						<fieldset class="layui-elem-field layui-field-title">
							<legend>基本信息</legend>
						</fieldset>
						<div class="layui-form-item">
							<label class="layui-form-label"><span class="redSpan">*</span>角色名称</label>
							<div class="layui-input-block">
								<#if method=='add'>
									<input type="text" name="roleName" value="${roleBean.roleName!}" isValidate="true" notEmpty="true" leng="20" placeholder="请输入角色名称" class="layui-input" lay-verify="title">
								</#if>
								<#if method =='edit'>
									<input type="hidden" name="roleName" value="${roleBean.roleName!}">
									<span class="layui-input">${roleBean.roleName!}</span>
								</#if>
							</div>
						</div>
						<div class="layui-form-item">
							<label class="layui-form-label"><span class="redSpan">*</span>所属机构</label>
							<div class="layui-input-block">
								<#if method=='add'>
									<#if orgList?exists>
										<#list orgList as item>
											<input type="radio" name="orgId" readonly="readonly" isValidate="true" notEmpty="true" <#if (roleBean.orgId?? && roleBean.orgId == item.orgId)>checked</#if> value="${item.orgId!}" title="${item.orgName!}" lay-filter="chooseOrg"/>
										</#list>
									</#if>
								</#if>
								<#if method=='edit'>
									<#if orgList?exists>
										<#list orgList as item>
											<#if (roleBean.orgId?? && roleBean.orgId == item.orgId)>
												<span class="layui-input">${item.orgName!''}</span>
												<input type="hidden" name="orgId" value="${item.orgId!''}"/>
												<#break>
											</#if>
										</#list>
									</#if>
								</#if>
							</div>
						</div>
						<div class="layui-form-item">
							<label class="layui-form-label"><span class="redSpan">*</span>身份角色</label>
							<div class="layui-input-block">
								<#if roleTypeList?exists>
									<#list roleTypeList as dict>
										<input type="radio" name="roleType" value="${dict.code}"  title="${dict.name}" isValidate="true" notEmpty="true" <#if (roleBean.roleType?? && dict.code == roleBean.roleType!'')>checked</#if>/>
									</#list>
								</#if>
							</div>
						</div>
					</div>
					<!-- 添加信息 Form End -->
					<!-- 标题 分割线 Start -->
					<fieldset class="layui-elem-field layui-field-title">
						<legend>菜单信息</legend>
					</fieldset>
					<!-- 标题 分割线 End -->
					<!-- 按钮 Start -->
					<blockquote class="layui-elem-quote">
						<div class="layui-btn-group">
							<#if (Session.CUR_MENU.addRole==1 && method=='add') || (Session.CUR_MENU.updateRole==1 && method=='edit')>
								<input type="button" class="layui-btn" onclick="saveRole();" value="保存">
							</#if>	
							<input type="button" class="layui-btn layui-btn-primary" onclick="back();" value="返回">
						</div>
			        </blockquote>
					<!-- 按钮 End -->
					<!-- 添加信息 Form Start -->
					<div class="container">
						<div id="div-message">${message!}</div>
						<div class="data-grid-group">
							<table lay-filter="data-grid" class="layui-table dymanic-table" id="old-table">
								<thead>
									<th lay-data="{field:'menuName', align: 'center', width: '28%'}">菜单名称</th>
									<th lay-data="{field:'selectRole', align: 'center', width: '12%'}">可见<input name="allSelectRole" lay-skin="primary" lay-filter="allChoose" type="checkbox"></th>
									<th lay-data="{field:'addRole', align: 'center', width: '12%'}">添加<input name="allAddRole" lay-skin="primary" lay-filter="allChoose" type="checkbox"></th>
									<th lay-data="{field:'updateRole', align: 'center', width: '12%'}">修改<input name="allUpdateRole" lay-skin="primary" lay-filter="allChoose" type="checkbox"></th>
									<th lay-data="{field:'deleteRole', align: 'center', width: '12%'}">删除<input name="allDeleteRole" lay-skin="primary" lay-filter="allChoose" type="checkbox"></th>
									<th lay-data="{field:'importRole', align: 'center', width: '12%'}">导入<input name="allImportRole" lay-skin="primary" lay-filter="allChoose" type="checkbox"></th>
									<th lay-data="{field:'exportRole', align: 'center', width: '12%'}">导出<input name="allExportRole" lay-skin="primary" lay-filter="allChoose" type="checkbox"></th>
								</thead>
								<tbody id="dataBody">
									<#if roleMenuList?exists>
										<#list roleMenuList as item>
											<#if item.selectRole == 1>
												<#assign var_checked_select = "checked"/>
											<#else>
												<#assign var_checked_select = ""/>
											</#if>
											<#if item.addRole == 1>
												<#assign var_checked_add = "checked"/>
											<#else>
												<#assign var_checked_add = ""/>
											</#if>
											<#if item.updateRole == 1>
												<#assign var_checked_update = "checked"/>
											<#else>
												<#assign var_checked_update = ""/>
											</#if>
											<#if item.deleteRole == 1>
												<#assign var_checked_delete = "checked"/>
											<#else>
												<#assign var_checked_delete = ""/>
											</#if>
											<#if item.importRole == 1>
												<#assign var_checked_import = "checked"/>
											<#else>
												<#assign var_checked_import = ""/>
											</#if>
											<#if item.exportRole == 1>
												<#assign var_checked_export = "checked"/>
											<#else>
												<#assign var_checked_export = ""/>
											</#if>
											<tr>
												<td>
													<div class="fn-hide">
														<input type="checkbox" class="fn-hide" name="menuIds" value="${item.menuId!}"/>
													</div>
													${item.menuName!}
												</td>
												<td>
													<input type="checkbox" lay-skin="primary" ${var_checked_select} name="selectRole" value="${item.menuId!}" />
												</td>
												<td>
													<input type="checkbox" lay-skin="primary" ${var_checked_add} name="addRole" value="${item.menuId!}" />
												</td>
												<td>
													<input type="checkbox" lay-skin="primary" ${var_checked_update} name="updateRole" value="${item.menuId!}" />
												</td>
												<td>
													<input type="checkbox" lay-skin="primary" ${var_checked_delete} name="deleteRole" value="${item.menuId!}" />
												</td>
												<td>
													<input type="checkbox" lay-skin="primary" ${var_checked_import} name="importRole" value="${item.menuId!}" />
												</td>
												<td>
													<input type="checkbox" lay-skin="primary" ${var_checked_export} name="exportRole" value="${item.menuId!}" />
												</td>
											</tr>
										</#list>
									</#if>
								</tbody>
							</table>
						</div>
					</div>
					<!-- 添加信息 Form End -->
				</form>
			</div>
		</div>
	</body>
</html>